﻿using Chromium;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace M5.GeminiHybird.Core.Builder
{
   public  class ContextMenuBuilder
    {
        private M5WebBrowser _m5WebBrowser { get; set; }

        private Rectangle _currScreen { get; set; }
        internal ContextMenuBuilder(M5WebBrowser m5WebBrowser)
        {
            _m5WebBrowser = m5WebBrowser;
            _currScreen = Screen.GetWorkingArea(_m5WebBrowser);
        }

        internal void Config()
        {
            _m5WebBrowser.ContextMenuHandler.RunContextMenu += MouseRightContextMenu;
            _m5WebBrowser.ContextMenuHandler.OnContextMenuCommand += MouseRightContextMenuItemClick;
        }

        /// <summary>
        /// 右键菜单点击后回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MouseRightContextMenuItemClick(object sender, Chromium.Event.CfxOnContextMenuCommandEventArgs e)
        {

            //开发者工具
            if (e.CommandId == 1)
            {
                CfxWindowInfo windowInfo = new CfxWindowInfo();
                windowInfo.Style = (WindowStyle.WS_OVERLAPPEDWINDOW | WindowStyle.WS_CLIPCHILDREN | WindowStyle.WS_CLIPSIBLINGS | WindowStyle.WS_VISIBLE);
                windowInfo.ParentWindow = IntPtr.Zero;
                windowInfo.WindowName = "开发者工具";
                windowInfo.X = 0;
                windowInfo.Y = _currScreen.Height - 500;
                windowInfo.Width = _currScreen.Width;
                windowInfo.Height = 500;
                _m5WebBrowser.BrowserHost.ShowDevTools(windowInfo, new CfxClient(), new CfxBrowserSettings(), null);
            }
            else if (e.CommandId == 2)
            {
                _m5WebBrowser.Browser.Reload();
            }
        }

        /// <summary>
        /// 向右键菜单添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MouseRightContextMenu(object sender, Chromium.Event.CfxRunContextMenuEventArgs e)
        {
            var browserMenuModel = e.Model;
            browserMenuModel.Clear();
            browserMenuModel.AddItem(1, "开发者工具");
            browserMenuModel.AddItem(2, "刷新");
        }
    }
}
